from django import forms
from .models import Story, StoryCategory
from django_ckeditor_5.widgets import CKEditor5Widget

class StoryForm(forms.ModelForm):
    class Meta:
        model = Story
        fields = ['title', 'content', 'category', 'keywords', 'is_draft']
        widgets = {
            'content': CKEditor5Widget(
                attrs={'class': 'django_ckeditor_5'},
                config_name='extends'
            ),
            'keywords': forms.TextInput(attrs={'placeholder': '用逗号分隔的关键词'}),
        }
        labels = {
            'title': '标题',
            'content': '内容',
            'category': '分类',
            'keywords': '关键词',
            'is_draft': '保存为草稿',
        }

class StoryGenerateForm(forms.Form):
    title = forms.CharField(
        max_length=100,
        label='故事标题',
        required=False,
        widget=forms.TextInput(attrs={'placeholder': '可选，留空则由AI生成'}),
        help_text='输入自定义故事标题，留空则由AI自动生成'
    )
    category = forms.ModelChoiceField(
        queryset=StoryCategory.objects.all().order_by('order', 'name'),
        label='故事类型',
        widget=forms.Select(attrs={'class': 'form-select'}),
        help_text='选择故事的分类类型'
    )
    keywords = forms.CharField(
        max_length=200,
        label='关键词',
        widget=forms.TextInput(attrs={'placeholder': '例如：魔法, 冒险, 友谊'}),
        help_text='输入关键词来指导AI生成故事，关键词越具体，生成的故事越符合期望'
    )
    length = forms.ChoiceField(
        choices=[
            ('short', '短篇'),
            ('medium', '中篇'),
            ('long', '长篇'),
        ],
        label='故事长度',
        widget=forms.Select(attrs={'class': 'form-select'})
    )
    style = forms.ChoiceField(
        choices=[
            ('casual', '轻松随意'),
            ('formal', '正式严谨'),
            ('poetic', '诗意优美'),
            ('humorous', '幽默风趣'),
        ],
        label='写作风格',
        widget=forms.Select(attrs={'class': 'form-select'})
    )

class StorySearchForm(forms.Form):
    """故事搜索表单"""
    q = forms.CharField(
        label='搜索',
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': '搜索故事标题、内容或关键词'
        })
    )
    category = forms.ModelChoiceField(
        label='分类',
        queryset=StoryCategory.objects.all(),
        required=False,
        empty_label='全部分类',
        widget=forms.Select(attrs={'class': 'form-select'})
    )
    sort_by = forms.ChoiceField(
        label='排序',
        choices=[
            ('-created_at', '最新发布'),
            ('-views', '最多浏览'),
            ('-likes', '最多点赞'),
        ],
        required=False,
        initial='-created_at',
        widget=forms.Select(attrs={'class': 'form-select'})
    ) 